<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;
//模型
use App\Models\Admin;
use App\Models\Role;
use App\Models\Authrole;
use Illuminate\Support\Facades\DB;

class RoleController extends PublicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $info = Role::where('agent_id',$this->agent_id) -> paginate(10);
        return view('admin.role.index',['data'=>$info,'request'=>$request -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['agent_id'] = $this->agent_id;
        //添加区域表
        $res = Role::create($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = Role::find($id) -> toarray();
        return view('admin.role.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        $res = Role::find($id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    public function getUrp(Request $request){
        $get['roleId'] = $request -> input('id');
        //获取访问链接
        $roleInfo = DB::table('role') -> find($get['roleId']);
        $urp = explode(',',$roleInfo['auth_ids']);
        //查询权限表
        $authInfo = $this -> authInfo();
        return view('admin.role.urp',['authInfo'=>$authInfo,'get'=>$get,'urp'=>$urp]);
    }

    public function postUrp(Request $request){
        $id = $request -> input('roleId');
        $authArr = $request -> input('authArr');
        if($authArr){
            $authStr = implode(',',$authArr);
        }else{
            $authStr = '';
        }
        //修改角色表
            $res = Role::find($id) -> update(['auth_ids'=>$authStr]);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    public function getAdm(Request $request){
        $get['roleId'] = $request -> input('id');
        //获取管辖区域
        $roleInfo = DB::table('role') -> find($get['roleId']);
        $adm = explode(',',$roleInfo['area_ids']);
        //查询区域表
        $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        return view('admin.role.adm',['areaInfo'=>$areaInfo,'get'=>$get,'adm'=>$adm]);
    }

    public function postAdm(Request $request){
        $id = $request -> input('roleId');
        $areaArr = $request -> input('areaArr');
        if($areaArr){
            $areaStr = implode(',',$areaArr);
        }else{
            $areaStr = '';
        }
        //修改角色表
            $res = Role::find($id) -> update(['area_ids'=>$areaStr]);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询区域下是否有建筑,有不能删除
        //$count = Admin::where('role_id',$id) -> count();
        //if($count == 0){
            //查询区域和客户的关系表并删除
            $res = Role::destroy($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        /*}else{
            $data['status'] = -1;
        }*/
        echo json_encode($data);
    }
}
